import { salesApi } from "@/utils/api";
// import { selectKey } from "element-plus";
// import { id } from "element-plus/es/locale";
interface IndexstateType{
    salesList:string[];
    salesclice:string[];
    currentPage:number;
    pageSize:number;
    pageTotal:number;
}
 const salesModule = {
    namespaced:true,
    state:{
        salesList:[],
        salesclice:[],
        currentPage: 1,
        pageSize: 10,
        pageTotal: 0,
    },
    mutations:{
        getsalesList(state:IndexstateType,payload?:any){
            state.salesList = payload;
            state.pageTotal = state.salesList.length;
            state.salesclice = state.salesList.slice((state.currentPage-1)*state.pageSize,state.currentPage*state.pageSize);
        },
        // 删除促销产品
        remove(state:IndexstateType,id:any){
            const i = state.salesList.findIndex((x:any)=>x.id ===id)
            if(i !== -1){
                state.salesList.splice(i,1);
            }
            state.pageTotal = state.salesList.length;
            state.salesclice = state.salesList.slice((state.currentPage-1)*state.pageSize,state.currentPage*state.pageSize);
        },
        // 批量终止
        bacth(state:IndexstateType){
            state.salesList.map((i:any)=>{
                // console.log(i)
                if(i.boo){
                    i.state = '已结束'
                }
            })
            state.pageTotal = state.salesList.length;
            state.salesclice = state.salesList.slice((state.currentPage-1)*state.pageSize,state.currentPage*state.pageSize);
        },
        // 批量删除
        c(state:IndexstateType){
            // console.log(state.salesList,1)
             state.salesList= state.salesList.filter((i:any)=> {
                return i.boo !== true
            } );
            state.pageTotal = state.salesList.length;
            state.salesclice = state.salesList.slice((state.currentPage-1)*state.pageSize,state.currentPage*state.pageSize);

        },
        // 查询
        check(state:IndexstateType,payload:any){
            // console.log(payload.value)
             state.salesList = state.salesList.filter((i:any)=>{
                if(i.id === payload.input){
                    // console.log(i);
                    return i;
                }else if(i.name === payload.name){
                    // console.log(i);
                    return i;
                }else if(i.state === payload.value){
                    console.log(payload.value)
                    // console.log(i)
                    return i
                }
            })
            state.pageTotal = state.salesList.length;
        state.salesclice = state.salesList.slice((state.currentPage-1)*state.pageSize,state.currentPage*state.pageSize);
        },
        // 修改完成
        complete(state:IndexstateType,payload:any){
            state.salesList.forEach((value:any)=>{
                if(value.id === payload.row.id){
                    value.name = payload.put.name;
                    value.relation = payload.put.relation
                    value.start = payload.put.start
                    value.end = payload.put.end
                    value.activity = payload.put.activity
                    value.state = payload.put.state
                    value.num = payload.put.num
                }
                // console.log(value.name)
                // console.log(payload.put.name)
            })
            state.pageTotal = state.salesList.length;
            state.salesclice = state.salesList.slice((state.currentPage-1)*state.pageSize,state.currentPage*state.pageSize);
        },
        // 勾选全部
        all(state:IndexstateType,payload:any){
            // console.log(payload)
            // console.log(state.salesclice)
            // state.salesList.forEach((i:any)=>{
            //     if(state.salesList.length === payload.length){
            //         i.boo = true;
            //     }else{
            //         i.boo = false;
            //     }
            // })
            state.salesclice.forEach((i:any)=>{
                if(state.salesclice.length === payload.length){
                    i.boo = true;
                }else{
                    i.boo = false;
                }
            })
            // console.log(state.salesclice.length);
            console.log(payload)
        },
        // 条数
        handelsize(state:IndexstateType,payload:any){
            state.pageSize = payload;
            state.salesclice = state.salesList.slice((state.currentPage-1)*state.pageSize,state.currentPage*state.pageSize);
        },
        handelchange(state:IndexstateType,payload:any){
            state.currentPage = payload;
            state.salesclice = state.salesList.slice((state.currentPage-1)*state.pageSize,state.currentPage*state.pageSize);
            console.log(state.salesclice);
        }
    },
    actions:{
       async getsalesAsync({commit}:any){
            const res = await salesApi();
            commit("getsalesList",res.result)
            // console.log(res.result);
       }
    },
}

export default salesModule;